//
//  FunnyPicViewController.m
//  噗嗤
//
//  Created by 火火火火火 on 16/8/16.
//  Copyright © 2016年 cjp. All rights reserved.
//

#import "FunnyPicViewController.h"
#import "FunnyTableViewCell.h"
#import <AFNetworking.h>
#import "FunnyModel.h"
#import <MJRefresh.h>
#import <MJExtension.h>
#import <UIImageView+WebCache.h>

@interface FunnyPicViewController ()<UITableViewDelegate , UITableViewDataSource>

@property (strong , nonatomic) UITableView * tableview;
@property (strong , nonatomic) NSMutableArray * dataArr;


@end

@implementation FunnyPicViewController

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableview = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableview];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    //    注册cell
    [self.tableview registerNib:[UINib nibWithNibName:@"FunnyTableViewCell" bundle:nil] forCellReuseIdentifier:@"funnycell"];
    [self parse];
}

//解析数据
- (void)parse
{
    NSURL * url  = [NSURL URLWithString:LeeFunnyURL];
    NSURLSession * session = [NSURLSession sharedSession];
    
    NSURLSessionTask * task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"请求失败 , %@" , error);
        }else{
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
            
            for (NSDictionary * dict in dic[@"items"]) {
                FunnyModel * funny = [FunnyModel new];
                [funny setValuesForKeysWithDictionary:dict];
                [self.dataArr addObject:funny];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableview reloadData];
            });
        }
    }];
    
    [task resume];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FunnyModel * model = self.dataArr[indexPath.row];
    
    return model.cellHeight;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FunnyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"funnycell"];

    FunnyModel  * funny = self.dataArr[indexPath.row];
    
//    时间格式转化
    NSTimeInterval second = funny.update_time.longLongValue;
    NSDate * date  = [NSDate dateWithTimeIntervalSince1970:second];
    NSDateFormatter * format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"MM-dd HH:mm";
    cell.dateLabel.text = [format stringFromDate:date];
    
    cell.contentLabel.text = funny.wbody;
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:funny.wpic_middle] placeholderImage:nil];
    
    [cell.praiseBtn setTitle:[NSString stringWithFormat:@"%.f" , funny.likes] forState:(UIControlStateNormal)];
    
    return cell;
}

@end
